<template>
  <div class="app-container">
    <div class="mainbox">
      <div class="filter-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
          <el-form-item label="客服人员" prop="realName">
            <el-input v-model.trim="realName" placeholder="请输入客服人员"></el-input>
          </el-form-item>
          <el-form-item label="通讯日期" prop="problem">
            <el-input v-model.trim="queryParams.accessTime" placeholder="请输入通讯日期"></el-input>
          </el-form-item>
          <el-form-item label="通讯信息关键字" prop="msg" label-width="128px">
            <el-input v-model.trim="queryParams.mag" placeholder="请输入关键字"></el-input>
          </el-form-item>
        </el-form>
        <div class="flex-1"></div>
        <div>
          <div class="flex-1"></div>
          <div style="display: flex">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
          </div>
        </div>
      </div>
      <div class="table-container table-fullscreen">
        <!-- 增删改查操作按钮 -->
        <div class="table-opt-container">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-permission="['ACCESSINFO_ADD_BUTTON']"
            >新增</el-button>
          <el-button
            type="success"
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-permission="['ACCESSINFO_UPDATE_BUTTON']"
            >修改</el-button >
          <el-button
            type="danger"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-permission="['ACCESSINFO_DEL_BUTTON']"
            >删除</el-button>
          <el-button
            type="warning"
            icon="el-icon-download"
            @click="handleExport"
            v-permission="['ACCESSINFO_EXPORT_BUTTON']"
            >导出</el-button>
          <div class="flex-1"></div>
          <dt-dialog-column
            v-model="isShowTable"
            :columns="showColumns"
            :table-ref="$refs.table"
            @queryTable="getList"/>
        </div>
        <!-- 数据表 -->
        <el-table v-if="isShowTable" v-loading="loading" ref="table" 
          border highlight-current-row :header-cell-style="{ backgroundColor: '#f2f2f2' }" 
          :data="AccessInfoList" @selection-change="handleSelectionChange">
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column fixed="left" type="selection" width="55" align="center"/>
          <el-table-column fixed="left" type="index" label="序号" width="70"
            :index="(index) =>(queryParams.pageNo - 1) * queryParams.pageSize + index + 1 "/>
          <!-- <el-table-column label="信息分类"
            v-if="showColumns[0].show" show-overflow-tooltip key="type" align="center" prop="type"/> -->
          <el-table-column label="通讯信息"
            v-if="showColumns[1].show" show-overflow-tooltip key="info" align="center" prop="info"/>
          <el-table-column label="通讯日期"
            v-if="showColumns[3].show" show-overflow-tooltip key="accessTime" align="center" prop="accessTime" width="180">
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.accessTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="客服人员"
            v-if="showColumns[2].show" show-overflow-tooltip key="realName" align="center" prop="realName"/>
          <el-table-column label="操作" :key="Math.random()" fixed="right" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="small" icon="el-icon-s-comment" @click="handleDetailChat(scope.row)" 
                v-permission="['ACCESSINFO_RECORD_BUTTON']">记录查看</el-button>
              <el-button size="small" icon="el-icon-edit" @click="handleUpdate(scope.row)" 
                v-permission="['ACCESSINFO_UPDATE_BUTTON']">修改</el-button >
              <el-button size="small" icon="el-icon-delete" @click="handleDelete(scope.row)" 
                v-permission="['ACCESSINFO_DEL_BUTTON']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <dt-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 添加或修改客服信息接入对话框 -->
    <!-- <dt-dialog :title="title" :visible.sync="open" :is-loading="loading" @comfirmBtn="submitForm" @closeDialog="cancel">
      <div slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="发送信息id(chat_tourist_user表id)" prop="sendId">
            <el-input
              v-model.trim="form.sendId"
              placeholder="请输入发送信息id(chat_tourist_user表id)"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="信息分类 1-用户 2-游客" prop="type">
            <el-select
              v-model="form.type"
              placeholder="请选择信息分类 1-用户 2-游客"
            >
              <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
          <el-form-item label="用户或游客发送的信息(仅有接入前)" prop="info">
            <el-input
              v-model.trim="form.info"
              type="textarea"
              maxlength="200"
              placeholder="请输入内容"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="接入人" prop="accessUser">
            <el-input
              v-model.trim="form.accessUser"
              placeholder="请输入接入人"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="接入时间" prop="accessTime">
            <el-date-picker
              clearable
              v-model="form.accessTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择接入时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="最后处理人" prop="lastDealUser">
            <el-input
              v-model.trim="form.lastDealUser"
              placeholder="请输入最后处理人"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="最后处理时间" prop="lastDealTime">
            <el-date-picker
              clearable
              v-model="form.lastDealTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择最后处理时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否删除：Y-已删除，N-未删除" prop="delFlag">
            <el-input
              v-model.trim="form.delFlag"
              placeholder="请输入是否删除：Y-已删除，N-未删除"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              clearable
              v-model="form.createTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择创建时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="创建人" prop="createUser">
            <el-input
              v-model.trim="form.createUser"
              placeholder="请输入创建人"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
          <el-form-item label="修改时间" prop="updateTime">
            <el-date-picker
              clearable
              v-model="form.updateTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择修改时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="修改人" prop="updateUser">
            <el-input
              v-model.trim="form.updateUser"
              placeholder="请输入修改人"
              maxlength="30"
              show-word-limit
              class="limit"
            />
          </el-form-item>
        </el-form>
      </div>
    </dt-dialog> -->
    <!-- 详情弹框 -->
    <dt-detail ref="dtDetailRef" :dt-detail-option="detailOption" />
    <!-- 记录详情 -->
    <el-dialog :title="titleChat" :visible.sync="openChat" width="600px" @close="handleChatClosed" append-to-body>
      <el-divider></el-divider>
      <div class="message_list" style="height: 462px;overflow-y: auto;">
        <!-- 没有聊天记录显示 -->
        <div v-if="messageDisabled" style="font-size: 80px;color: #cccccc;text-align: center;margin-top: 30px;">
          <el-image style="width: 170px; height: 180px" :src="require('@/framework/assets/empty__image@2x.png')" />
          <p style="padding: 0; margin: 0; font-size: 14px">暂无通讯记录</p>
        </div>
        <!-- 聊天记录列表 -->
        <div class="message_item" v-for="(item,index) in chatMessageList" :key="index" :style="item.sendUserId == nowUserId ? 'color:#303133' : 'color:aqua;'">
          <div class="item_obj">
            <div class="item_name">{{item.sendUserName}}</div>
            <div class="item_time">{{item.sendTime}}</div>
          </div>
          <div class="item_msg">
            {{item.msg}}
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAccessInfo,
  getAccessInfo,
  delAccessInfo,
  addAccessInfo,
  getCommunicateMessageApi,
  updateAccessInfo
} from "../../../api/accessInfo/AccessInfo";
import store from "@/store";
export default {
  name: "AccessInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      //显隐表格
      isShowTable: true,
      // 选中数组
      ids: [],
      //列显隐数组
      showColumns: [
        { prop: "type", label: "信息分类", show: true },
        { prop: "info", label: "通讯消息", show: true },
        { prop: "realName", label: "客服人员", show: true },
        { prop: "accessTime", label: "通讯时间", show: true },
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客服信息接入表格数据
      AccessInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        accessUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      titleChat: "",
      openChat: false,
      messageDisabled: false,
      realName: null,
      chatMessageList: [
      ],
      nowUserId:store.state.user.userId,
    };
  },
  computed: {
    //详情参数
    detailOption() {
      return {
        rows: [
          {
            label: "发送信息id(chat_tourist_user表id)",
            prop: "sendId",
          },
          {
            label: "信息分类 1-用户 2-游客",
            prop: "type",
          },
          {
            label: "用户或游客发送的信息(仅有接入前)",
            prop: "info",
          },
          {
            label: "接入人",
            prop: "accessUser",
          },
          {
            label: "接入时间",
            prop: "accessTime",
          },
          {
            label: "最后处理人",
            prop: "lastDealUser",
          },
          {
            label: "最后处理时间",
            prop: "lastDealTime",
          },
          {
            label: "是否删除：Y-已删除，N-未删除",
            prop: "delFlag",
          },
          {
            label: "创建时间",
            prop: "createTime",
          },
          {
            label: "创建人",
            prop: "createUser",
          },
          {
            label: "修改时间",
            prop: "updateTime",
          },
          {
            label: "修改人",
            prop: "updateUser",
          },
        ],
        data: {}
      };
    },
  },
  created() {

    this.getList();
  },
  methods: {
    /** 查询客服信息接入列表 */
    getList() {
      this.loading = true;
      var search = this.queryParams;
      if (this.realName != null && this.realName != "" && this.realName != undefined) {
	      search.otherParams = {};
        search.otherParams["realName"] = this.realName;
      }
      listAccessInfo(search).then(({ data: response }) => {
        this.AccessInfoList = response.rows;
        this.total = response.totalRows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sendId: null,
        type: null,
        info: null,
        accessUser: null,
        accessTime: null,
        lastDealUser: null,
        lastDealTime: null,
        delFlag: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.resetqueryForm();
      this.handleQuery();
    },
    resetqueryForm(){
      this.queryParams={
        pageNo: 1,
        pageSize: 10,
      }
      this.realName = null;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 关闭dialog窗回调，重置数据
    handleChatClosed(){
      this.chatMessageList = null;
      this.messageDisabled = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客服信息接入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAccessInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客服信息接入";
      });
    },
    handleDetail(row) {
      this.detailOption.data = row;
      this.$refs.dtDetailRef.dialogVisible = true;
    },
    // 记录详情查看
    handleDetailChat(row){
      if (row.accessUser == null || row.accessUser == "" || row.accessUser == undefined) {
        this.messageDisabled = true;
        this.titleChat = "与游客"+row.sendId+"的聊天记录";
        this.openChat = true;
      } else {
        let dataParams = {};
        dataParams.sendId = row.sendId;
        dataParams.accessUser = row.accessUser;
        dataParams.info = row.info;
        this.loading = true;
        // 1.拿到游客与客服id，查询groupId，根据组id查询聊天信息
        getCommunicateMessageApi(dataParams).then(res => {
          if (res.code == "00000") {
            if (res.data.length <= 0) {
              this.messageDisabled = true;
            }
            this.loading = false;
            this.chatMessageList = res.data;
            this.titleChat = "与游客"+row.sendId+"的聊天记录";
            this.openChat = true;
          }
        }).catch(err =>{
          this.$message.error("获取记录信息失败，请稍后再试！")
        })
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAccessInfo(this.form).then((response) => {
              this.$dtModal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccessInfo(this.form).then((response) => {
              this.$dtModal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$dtModal
        .confirm('是否确认删除客服信息接入编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAccessInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$dtModal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "accessInfo/AccessInfo/export",
        {
          ...this.queryParams,
        },
        `accessInfo_AccessInfo.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
  ::v-deep .el-dialog .el-dialog__header {
    padding: 10px 20px 0px 20px !important;
  }
  ::v-deep .el-dialog__body {
    padding: 0px 20px 10px 20px !important;
  }
  ::v-deep .el-dialog__headerbtn {
    top: 10px !important;
  }
  .el-divider--horizontal {
    border-bottom: 1px solid #303133;
  }
  .el-divider--horizontal {
    margin: 15px 0;
  }
  .message_item {
    font-size: 16px;
    margin: 3px 3px 10px 2px;
    color: #303133;
    .item_obj {
      display: flex;
      align-items: center;
      .item_name {
        margin-right: 10px;
      }
    }
  }
</style>
